package com.jzy.util;

import com.jzy.constant.DateConstants;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * @author 姬海威 [haiwei.ji@tigermed.net]
 * @Date 2017-6-13 16:57
 */
public class DateUtils {

    /**
     * 字符串转换成日期
     */
    public static Date stringToDate(String date, String formatStr) throws Exception {
        DateFormat df = new SimpleDateFormat(formatStr);
        return (Date) df.parseObject(date);
    }

    /**
     * 字符串转换成日期(yyyy-MM-dd)
     */
    public static Date stringToDate(String date) throws Exception {
        DateFormat df = new SimpleDateFormat(DateConstants.FORMAT_DATE);
        return (Date) df.parseObject(date);
    }

    /**
     * 日期转换成字符串
     */
    public static String dateToString(Date date, String formatStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        return sdf.format(date);
    }

    /**
     * 日期转换成字符串(yyyy-MM-dd)
     */
    public static String dateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DateConstants.FORMAT_DATE);
        return sdf.format(date);
    }

    /**
     * 计算两个日期之间相差的天数
     */
    public static int daysBetween(Date smallDate, Date bigDate) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(DateConstants.FORMAT_DATE);
        smallDate = sdf.parse(sdf.format(smallDate));
        bigDate = sdf.parse(sdf.format(bigDate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smallDate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bigDate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / DateConstants.DAY_MILLI_SECONDS;

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 获取一定分钟后的时间
     */
    public static Date addMinute(Date date, int minuteCount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minuteCount);
        return calendar.getTime();
    }

    /**
     * 获取一定小时后的时间
     */
    public static Date addHour(Date date, int hourCount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, hourCount);
        return calendar.getTime();
    }

    /**
     * 获取一定月份后的时间
     */
    public static Date addMonth(Date date, int monthCount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, monthCount);
        return calendar.getTime();
    }

    /**
     * 获取一定年份后的时间
     */
    public static Date addYear(Date date, int yearCount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, yearCount);
        return calendar.getTime();
    }

    /**
     * 时间戳转化为指定格式字符串
     */
    public static String formatLongDate(Long time, String formatStr) throws Exception {
        Timestamp ts = new Timestamp(time);
        DateFormat sdf = new SimpleDateFormat(formatStr);
        return sdf.format(ts);
    }

    /**
     * @Description: 获取本周日日期
     */
    public static String getSundayOfThisWeek() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateConstants.FORMAT_DATE);
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 7);
        return simpleDateFormat.format(c.getTime());
    }

    /**
     * @Description: 得到本月第一天
     */
    public static String getCurrentMonthFirstDay() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateConstants.FORMAT_DATE);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        return simpleDateFormat.format(c.getTime());
    }

    /**
     * @Description: 得到本月最后一天
     */
    public static String getCurrentMonthLastDay() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateConstants.FORMAT_DATE);
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return simpleDateFormat.format(c.getTime());
    }

    /**
     * @Description: 得到当前日期
     */
    public static String currentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat(DateConstants.FORMAT_DATE);
        return sdf.format(new Date());
    }

    /**
     * @Description: 得到当前时间
     */
    public static String currentDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat(DateConstants.FORMAT_DATE_TIME);
        return sdf.format(new Date());
    }

    /**
     * 比较一个时间是否在指定的时间段内
     */
    public static boolean belongCalendar(Date time, Date from, Date to) {
        Calendar date = Calendar.getInstance();
        date.setTime(time);

        Calendar after = Calendar.getInstance();
        after.setTime(from);

        Calendar before = Calendar.getInstance();
        before.setTime(to);
        return date.compareTo(after) >= 0 && date.compareTo(before) <= 0;
    }

    /**
     * 获取当前时间的年月值
     */
    public static String getCurrentYeanAndMonth() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        return String.valueOf(year) + String.valueOf(month);
    }

    /**
     * 指定数字转化成日期
     * 如10-周一上午 11-周一下午
     * 20-周二上午 21周二下午 ....第一位数字代表周X 第二位数字0-上午 1-下午
     */
    public static String number2DateString (String numberDate) {
        StringBuffer result = new StringBuffer();
        if (StringUtils.isNotEmpty(numberDate) && numberDate.length()== 2) {
            String week = numberDate.split("")[0];
            String noon = numberDate.split("")[1];

            result.append(formatWeek(week)).append(formatNoon(noon));
        }

        return result.toString();
    }

    private static String formatWeek (String week) {
        switch (week) {
            case "1" :
                return "周一";
            case "2" :
                return "周二";
            case "3" :
                return "周三";
            case "4" :
                return "周四";
            case "5" :
                return "周五";
            case "6" :
                return "周六";
            case "7" :
                return "周日";
        }
        return null;
    }

    private static String formatNoon (String noon) {
        switch (noon) {
            case "0" :
                return "上午";
            case "1" :
                return "下午";
        }
        return null;
    }
}
